package com.example.recordingpen_datacollection.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.recordingpen_datacollection.Utils.MD5Util.MD5EncryptionExample;
import com.example.recordingpen_datacollection.Utils.SerialNumberUtil;
import com.example.recordingpen_datacollection.Utils.String.StringUtils;
import com.example.recordingpen_datacollection.entity.LimitRatio;
import com.example.recordingpen_datacollection.entity.Query.LndividualOrderQue;
import com.example.recordingpen_datacollection.entity.Query.RequestAnOrderQue;
import com.example.recordingpen_datacollection.entity.Requestanorder;
import com.example.recordingpen_datacollection.entity.UserMessage;
import com.example.recordingpen_datacollection.entity.Userdevmess;
import com.example.recordingpen_datacollection.entity.Vo.RequestanorderCertaintyVo;
import com.example.recordingpen_datacollection.entity.Vo.RequestanorderVo;
import com.example.recordingpen_datacollection.mapper.RequestanorderMapper;
import com.example.recordingpen_datacollection.result.RLIST;
import com.example.recordingpen_datacollection.result.exceptionhandler.JWException;
import com.example.recordingpen_datacollection.service.LimitRatioService;
import com.example.recordingpen_datacollection.service.RequestanorderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.recordingpen_datacollection.service.UserMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xg
 * @since 2024-06-07
 */
@Service
public class RequestanorderServiceImpl extends ServiceImpl<RequestanorderMapper, Requestanorder> implements RequestanorderService {

    @Autowired
    private LimitRatioService limitRatioService;
    @Autowired
    private UserMessageService userMessageService;

    @Override
    public boolean setRequestanorder(RequestanorderVo requestanorderVo, Integer value) {
        Requestanorder requestanorder = new Requestanorder();
        requestanorder.setUid(value);
        requestanorder.setLimit(requestanorderVo.getLimit());

        String serialNumber = SerialNumberUtil.GenerateTheSequenceNumber(System.currentTimeMillis(),value);
        requestanorder.setSerialNumber(serialNumber);
        boolean save = this.save(requestanorder);
        return save;
    }

    @Override
    @Transactional
    public boolean alterRequestanorderCertainty(Requestanorder requestanorder) {

        Requestanorder requestanorder2 = new Requestanorder();
        requestanorder2.setApid(requestanorder.getApid());
        requestanorder2.setOrderStatus(1);
        boolean update = this.updateById(requestanorder2);
        if (!update) {
            return false;
        }
        UserMessage byId = userMessageService.getById(requestanorder.getUid());
        byId.setLimitQuantity(byId.getLimitQuantity()+requestanorder.getLimit());
        byId.setTotalAmount(byId.getTotalAmount()+requestanorder.getLimit());
        boolean update1 = userMessageService.updateById(byId);
        if(!update1){
            throw new JWException(500,"数据更新失败");
        }
        return update1;
    }

    @Override
    public RLIST getRequestAnOrderPage(RequestAnOrderQue requestAnOrderQue) {
        QueryWrapper wrapper = new QueryWrapper();
        //校验

        if(requestAnOrderQue.getUid() !=null && requestAnOrderQue.getUid() >0){
            wrapper.eq("uid",requestAnOrderQue.getUid());
        }
        if(StringUtils.isNotEmpty(requestAnOrderQue.getOrderNumber())){
            wrapper.like("serial_number",requestAnOrderQue.getOrderNumber());
        }

        wrapper.orderByDesc("apid");
        Page<Requestanorder> page = new Page<>(requestAnOrderQue.getPageNo(), requestAnOrderQue.getPageSize());
        this.page(page, wrapper);
        long total = page.getTotal();
        List<Requestanorder> list = page.getRecords();

        long numPage = 0;
        if(total == 0){
            numPage = 1L;
        } else if(list.size() == 0){
            numPage = 1L;
        } else {
            // 每页条数
            numPage = (long) Math.ceil(total / (double)requestAnOrderQue.getPageSize()) ;
        }
        if(list.size() == 0){
            return RLIST.error().message("暂无数据") ;
        }
        return RLIST.ok().data("data",list).data("total",total).data("numPage",numPage);

    }

    @Override
    public RLIST getPersonnelOrdersPage(LndividualOrderQue lndividualOrderQue, Integer value) {
        QueryWrapper wrapper = new QueryWrapper();
        if(StringUtils.isNotEmpty(lndividualOrderQue.getOrderNumber())){
            wrapper.like("serial_number",lndividualOrderQue.getOrderNumber());
        }
        wrapper.eq("uid",value);
        wrapper.orderByDesc("apid");
        Page<Requestanorder> page = new Page<>(lndividualOrderQue.getPageNo(), lndividualOrderQue.getPageSize());
        this.page(page, wrapper);
        long total = page.getTotal();
        List<Requestanorder> list = page.getRecords();

        long numPage = 0;
        if(total == 0){
            numPage = 1L;
        } else if(list.size() == 0){
            numPage = 1L;
        } else {
            // 每页条数
            numPage = (long) Math.ceil(total / (double)lndividualOrderQue.getPageSize()) ;
        }
        if(list.size() == 0){
            return RLIST.error().message("暂无数据") ;
        }
        return RLIST.ok().data("data",list).data("total",total).data("numPage",numPage);
    }
}
